package com.example.demo.Util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @description
 *
 * @author lilinchun
 * @date 2024/03/26
 */
@Component
@Slf4j
public class ListHandleUtil {

    /**
     * 拆分集合
     *
     * @param list list
     * @param len  拆分长度
     * @return list
     */
    public static <T> List<List<T>> splitList(List<T> list, int len) {
        if (list == null || list.size() == 0 || len < 1) {
            return null;
        }
        List<List<T>> result = new ArrayList<>();

        int size = list.size();
        int count = (size + len - 1) / len;

        for (int i = 0; i < count; i++) {
            // 开始位置
            int fromIndex = i * len;
            // 结束位置
            int toIndex = Math.min((i + 1) * len, size);
            result.add(list.subList(fromIndex, toIndex));
        }
        return result;
    }
}
